<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        body {
            height: 2000px;
        }

        .box {
            position: relative;
            top: 200px;
            left: 200px;
            /* margin-top: 100px;
            margin-left: 100px; */
            width: 100px;
            height: 100px;
            background-color: aquamarine;
        }
    </style>
</head>

<body>
    <div class="box"></div>
</body>
<script>
    var box = document.getElementsByClassName('box')[0];
    box.onmousedown = function (e) {
        //计算盒子左上角距离边框的距离
        console.log(e.pageX - e.offsetX, e.pageY - e.offsetY);

        document.onmousemove = function (e1) {
            //鼠标距离顶部的位置-鼠标距离盒子的距离
            box.style.top = e1.pageY - e.offsetY + 'px';
        }
        document.onmouseup = function (e) {
            document.onmousemove = null;
        }
    }

</script>

</html>